<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Usergroup extends Controller_Admin_Main
{

    private $usergroup;

    public function before()
    {
        parent::before();
        $this->usergroup = Model::factory('user_group');
        I18n::set_current_file('privilege');
    }

    public function action_list()
    {
        $group_list = $this->usergroup->get_group_list();
        $this->template->assign('group_list', $group_list);
        $this->template->assign('action', URL::site() . 'admin/usergroup/batch_position/');
        $this->template->assign('transfer_action', URL::site() . 'admin/usergroup/transfer/');
        $this->template->assign('select_group_list', $this->usergroup->select_group_list());
        $this->template->view('user_group_list');
    }

    public function action_add()
    {
        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['en_name'] = Arr::get($_POST, 'en_name', '');
            $data['experience'] = Arr::get($_POST, 'experience', 0);
            $data['star_num'] = Arr::get($_POST, 'star_num', 0);
            $data['active'] = Arr::get($_POST, 'active', 0);
            $data['color'] = Arr::get($_POST, 'color', '');
            $data['description'] = Arr::get($_POST, 'description', '');
            $data['position'] = Arr::get($_POST, 'position', 9999);
            if (!$this->usergroup->validate($data))
            {
                Message::error(__('usergroup validate error'));
                $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'add')));
            }
            if ($this->usergroup->is_exist(array('name' => $data['name'])))
            {
                Message::set(Message::ERROR, "\"" . $data['name'] . "\"" . '--用户组名称已经存在');
                $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'add')));
            }

            //处理上传的图标
            if (isset($_FILES))
            {
                $dir = ROOT_PATH . 'static/admin/images/user/';
                $uploads = Upload::instance($dir)->upload('icon', 'png|jpg|gif');
                if (count($uploads) > 0)
                {
                    $data['icon'] = $uploads[0]['file_real_name'];
                }
            }
            if ($this->usergroup->insert($data))
            {
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--用户组添加成功');
                $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'add')));
            }
        }
        Assets::css('color', $this->template->media_path . 'css/jquery.bigcolorpicker.css');
        Assets::js('color', $this->template->media_path . 'js/jquery.bigcolorpicker.min.js');
        $this->template->view('user_group_add');
    }

    public function action_edit()
    {
        Assets::css('color', $this->template->media_path . 'css/jquery.bigcolorpicker.css');
        Assets::js('color', $this->template->media_path . 'js/jquery.bigcolorpicker.min.js');
        $id = intval($this->request->param('id'));

        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['en_name'] = Arr::get($_POST, 'en_name', '');
            $data['experience'] = Arr::get($_POST, 'experience', 0);
            $data['star_num'] = Arr::get($_POST, 'star_num', 0);
            $data['active'] = Arr::get($_POST, 'active', 0);
            $data['color'] = Arr::get($_POST, 'color', '');
            $data['description'] = Arr::get($_POST, 'description', '');
            $data['position'] = Arr::get($_POST, 'position', 9999);
            if (!$this->usergroup->validate($data))
            {
                Message::error(__('validate error'));
                $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'add')));
            }

            if ($this->usergroup->is_exist(array('name' => $data['name']), $id))
            {
                Message::set(Message::ERROR, "\"" . $data['name'] . "\"" . '--用户组名称已经存在');
                $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'add')));
            }
            //处理上传的图标
            if (isset($_FILES) && !empty($_FILES['icon']['name']))
            {
                $dir = USER_ICON_DIR;
                $uploads = Upload::instance($dir)->upload('icon', 'png|jpg|gif');
                if (count($uploads) > 0)
                {
                    $data['icon'] = $uploads[0]['file_real_name'];
                }
            }

            if ($this->usergroup->update($data, array('id' => $id)))
            {
                $privilege_data = array();
                $privilege_data['group_id'] = $id;
                $privilege_data['privilege_id'] = $_POST['privilege_id'];
                if (Arr::validate_arr($privilege_data))
                {
                    $this->usergroup->save_group_privilege($privilege_data);
                }
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--用户组修改成功');
                $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'edit', 'id' => $id)));
            }
        }



        //获取缓存文件
        $privilege_list_temp = $this->get_cache_file('privilege');
        $privilege_list = $this->handle_privilege($privilege_list_temp);


        $this->template->assign('privilege_list', $privilege_list);
        $group_detail = $this->usergroup->get_usergroup_by_id($id);
        $this->template->assign('data', $group_detail);
        $this->template->view('user_group_edit');
    }

    public function handle_privilege($data)
    {
        $data_temp = array();
        foreach ($data as $key => $value)
        {
            if ($value['parent_id'] == 0)
            {
                foreach ($data as $key2 => $value2)
                {
                    if ($value2['parent_id'] == $key)
                    {
                        $value['children'][] = $value2;
                    }
                }
                $data_temp[] = $value;
            }
        }
        return $data_temp;
    }

    public function action_delete()
    {
        $id = intval($this->request->param('id'));

        //判断是否存在
        if (!$this->usergroup->is_exist(array('id' => $id)))
        {
            Message::set(Message::ERROR, '用户组不存在');
            $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'list')));
        }
        //判断是否有用户所属本组
        if ($this->usergroup->get_count_user($id))
        {
            Message::set(Message::ERROR, '该用户下有用户，请先转移用户');
            $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'list')));
        }
        if ($this->usergroup->delete(array('id' => $id)))
        {
            Message::set(Message::SUCCESS, '用户组删除成功');
            $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'list')));
        }
    }

    public function action_transfer()
    {
        $from_id = intval($this->request->param('id'));
        $to_id = Arr::get($_POST, 'to_id', $from_id);
        if ($this->usergroup->transfer_user_group($from_id, $to_id))
        {
            Message::set(Message::SUCCESS, '用户转移成功');
            $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'list')));
        }
        Message::set(Message::ERROR, '用户组转移失败');
        $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'list')));
    }

    public function action_batch_position()
    {
        $position_arr = $_POST['position'];
        if (count($position_arr) > 0)
        {
            foreach ($position_arr as $key => $value)
            {
                $this->usergroup->update(array('position' => trim($value)), array('id' => $key));
            }
        }
        Message::set(Message::SUCCESS, '排序成功');
        $this->request->redirect(Route::get('admin/usergroup')->uri(array('action' => 'list')));
    }

    public function action_set_default()
    {
        $id = $this->request->param('id');
        
        $this->usergroup->update(array('is_default'=>0));
        
        $this->usergroup->update(array('is_default'=>1),array('id'=>$id));
        
        Message::success(__('update success'));
        $this->request->redirect('admin/usergroup/list');
        
    }

}